#include<bits/stdc++.h>
using namespace std;
#define N 200005
#define ll long long
ll f[N],g[N],t[N],pre[N],T,n,a[N];
inline ll read()
{
    ll x=0;char ch;
    ch=getchar();
    while(ch>'9'||ch<'0') ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x;
}
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    T=read();
    while(T--)
    {
        n=read();
        memset(f,0,sizeof(f));
        memset(g,0,sizeof(g));
        memset(t,0,sizeof(t));
        memset(pre,0,sizeof(pre));
        for(int i=1;i<=n;i++)
        {
            a[i]=read();
            if(t[a[i]])
                pre[i]=t[a[i]];
            t[a[i]]=i;
        }
        for(int i=2;i<=n;i++)
        {
            if(a[i-1]==a[i]) g[i]+=g[i-1]+a[i];
            else g[i]=g[i-1];
        }
        //for(int i=1;i<=n;i++) printf("g[%d]=%d\n",i,g[i]);
        //for(int i=1;i<=n;i++) printf("pre[%d]=%d\n",i,pre[i]);
        for(int i=1;i<=n;i++)
        {
            if(pre[i])
            {
                if(pre[i]>1&&pre[pre[i]+1]&&pre[i]!=i-1) f[i]=max(f[pre[i]]+a[i]+g[i-1]-g[pre[i]]+a[pre[i]+1],f[i-1]);
                else f[i]=max(f[pre[i]]+a[i]+g[i-1]-g[pre[i]],f[i-1]);
            }
            f[i]=max(f[i],f[i-1]);
        }
        /*for(int i=1;i<=n;i++)
            printf("f[%d]=%d\n",i,f[i]);*/
        printf("%lld\n",f[n]);
    }
    return 0;
}

